<?php

namespace App\Console\Commands;

use App\Models\ModelNumberSpecification;
use App\Models\Product;
use App\Models\ProductModelNumber;
use App\Models\Specification;
use Illuminate\Console\Command;

class AddSpecification extends Command
{
    /**
     * The name and signature of the console command.
     * php artisan command:addspecification
     * @var string
     */
    protected $signature = 'command:addspecification';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Command description';

    /**
     * Execute the console command.
     *
     * @return int
     */
    public function handle()
    {
        $ruanchuang_arr = [
            '1.5*2',
            '1.8*2',
            '2*2',
            '2*2.2'
        ];
        $shafa_arr = [
            '4人位对条','4人位1+1+1+1','4人位对条加踏','4人位1+1+1+1加踏','2扶单椅','2人位直条','2人位1+1','2人位直条加踏','2人位1+1加踏','3人位左大右小2+1','3人位左小右大1+2','3人位左大右小2+1加踏','3人位左小右大1+2加踏','3人位1+1+1','3人位1+1+1加踏','拉床左妃','拉床右妃','拉床左妃箱','拉床右妃箱','拉床条双扶','拉床活扶手','左妃二人单人扶手','单人扶手二人右妃','左妃二人+拉床单人扶手','单人扶手二人+拉床右妃','左妃箱二人单人扶手','单人扶手二人右妃箱','左妃箱二人+拉床单人扶手','单人扶手二人+拉床右妃箱','左妃二人单人扶手+电动','单人扶手二人右妃+电动','左妃二人+拉床单人扶手+电动','单人扶手二人+拉床右妃+电动','左妃箱二人单人扶手+电动','单人扶手二人右妃箱+电动','左妃箱二人+拉床单人扶手+电动','单人扶手二人+拉床右妃箱+电动','二人加扶手右妃','左妃二人加扶手','二人加扶手右妃箱','左妃箱二人加扶手','二人拉床加扶手右妃','左妃二人拉床加扶手','二人拉床加扶手右妃箱','左妃箱二人拉床加扶手','二人扶手加单人','二人扶手加单人右妃','左妃二人扶手加单人'
            ];
        $lachuangshafa_arr = [
                '单抽加箱2.8X1.7',
                '单抽加箱2.7X1.7',
                '单抽1.8',
                '双抽1.8',
                '活扶手单抽1.8',
                '单抽2',
                '双抽2',
                '活扶手单抽2',
                '单抽2.3',
                '双抽2.3',
                '活扶手单抽2.3',
                '单抽2.4',
                '双抽2.4',
                '活扶手单抽2.4',
                '单抽2.2',
                '双抽2.2',
                '活扶手单抽2.2',
                '单抽2.1',
                '双抽2.1',
                '活扶手单抽2.1'
        ];
        $product_list = Product::where('is_del',2)->get()->toArray();
        foreach($product_list as $k=>$v){
            $mnlist = ProductModelNumber::where('product_id',$v['id'])
                ->pluck('model_number_id')->toArray();
            foreach($mnlist as $kk=>$vv){
                //软床
                if($v['modelid'] == 2){
                    foreach($ruanchuang_arr as $kkk=>$vvv){
                        $spe = Specification::where('specification',$vv)->first();
                        if($spe){
                            $this->info('软床：'.$v['product_name']."==型号ID：".$vv."==规格：".$vvv);
                            ModelNumberSpecification::updateOrCreate(
                                [
                                    'product_id'=>$v['id'],
                                    'model_number_id'=>$vv,
                                    'specification_id'=>$spe->id
                                ],[
                                'product_id'=>$v['id'],
                                'model_number_id'=>$vv,
                                'specification_id'=>$spe->id
                            ]);
                        }
                    }
                }elseif ($v['modelid'] == 1){

                    $str = $v['product_name'];
                    $subStr = "拉床";

                    if (strpos($str, $subStr) !== false) {
                        // "拉床";
                        foreach($lachuangshafa_arr as $kkk=>$vvv){
                            $spe = Specification::where('specification',$vvv)->first();
                            if($spe){
                                $this->info('拉床：'.$v['product_name']."==型号ID：".$vv."==规格：".$vvv);

                                ModelNumberSpecification::updateOrCreate(
                                    [
                                        'product_id'=>$v['id'],
                                        'model_number_id'=>$vv,
                                        'specification_id'=>$spe->id
                                    ],[
                                    'product_id'=>$v['id'],
                                    'model_number_id'=>$vv,
                                    'specification_id'=>$spe->id
                                ]);
                            }
                        }
                    } else {
                        // "沙发";
                        foreach($shafa_arr as $kkk=>$vvv){
                            $spe = Specification::where('specification',$vvv)->first();
                            if($spe){
                                $this->info('沙发：'.$v['product_name']."==型号ID：".$vv."==规格：".$vvv);

                                ModelNumberSpecification::updateOrCreate(
                                    [
                                        'product_id'=>$v['id'],
                                        'model_number_id'=>$vv,
                                        'specification_id'=>$spe->id
                                    ],[
                                    'product_id'=>$v['id'],
                                    'model_number_id'=>$vv,
                                    'specification_id'=>$spe->id
                                ]);
                            }
                        }
                    }
                }
            }

        }
    }
}
